﻿

﻿using Iesi.Collections.Generic;
using Inovout.Cloud.Applications.Models;
using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Identity;
using Inovout.Cloud.Models;
using Inovout.Descriptors;
using Inovout.Memberships;
using Inovout.Memberships.Services;
using Inovout.RedictUrls;
using Inovout.Security;
using Inovout.Security.Services;
using Inovout.Services;
using Inovout.Web;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Inovout.Cloud.Console.Web
{
    public class MvcApplication : WebApplication
    {
        public override void Run()
        {
            base.Run();
            //new Inovout.Repositories.DatabaseInstaller().InstallSchema();

            //CreateDatabase();
            //ImportObjctOperationToEnumOperation();
            //CreateProductAndPrivilegeForIdentiry();
            //CreateProductAndPrivilegeForDataStore();
            //CreateProductAndPrivilegeForStorage();
            //CreateProductAndPrivilegeForDatabaseCluster();

        }

        private void CreateDatabase()
        {
            //Inovout.People.Models.Person p = new People.Models.Person();
            //Inovout.Settings.ObjectSettings s = new Settings.ObjectSettings();
            //Inovout.Cloud.DataStore.Models.Table t = new DataStore.Models.Table();
            //Inovout.Cloud.DataWarehouse.Models.DataCollection ds = new DataWarehouse.Models.DataCollection();
            //Inovout.Security.Permission pe = new Permission();
            //Inovout.Security.HttpResource h = new HttpResource();
            //Inovout.Cloud.Data.Blob.Models.Directory dir = new Data.Blob.Models.Directory();
            //Inovout.Cloud.Data.Blob.Models.File file = new Data.Blob.Models.File();
            //Inovout.Cloud.Storage.Models.Bucket bucket = new Storage.Models.Bucket();
            //Inovout.Cloud.Storage.Models.Object objectInBucket = new Storage.Models.Object();
            //Inovout.Cloud.DatabaseCluster.Models.Database DatabaseClusterDatabase = new DatabaseCluster.Models.Database();
            //RedirctUrl redirecturl = new RedirctUrl();
    

            Organizations.Memberships.Services.OrganizationMembershipService organizationMembershipService = ComponentRegistry.Resolve<Organizations.Memberships.Services.OrganizationMembershipService>();
            Cloud.Services.IProjectService projectService = ComponentRegistry.Resolve<Cloud.Services.IProjectService>();
            MembershipUserService membershipUserService = ComponentRegistry.Resolve<MembershipUserService>();
            SystemUser systemUser = new SystemUser();
            systemUser.Name = Guid.NewGuid().ToString();
            var membershipUser = organizationMembershipService.Create(
                new Organizations.Models.OrganizationUnit { Name = "3Cloud运营团队", Email = "huay1@samples.cn" },
                new Security.FormsUserCredential
                {
                    AccountName = "3Cloud运营团队",
                    Email = "huay1@samples.cn",
                    UserName = "3cloud",
                    Password = "123456",
                    Provider = "Inovout",
                    Identifier = Guid.NewGuid().ToString(),
                    SystemUser = systemUser
                }, MembershipUserStatus.Activate);
            membershipUser.Status = Memberships.MembershipUserStatus.Activate;
            membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);
            projectService.Save(new Inovout.Cloud.Models.Project { Name = "3cloud", Title = "三宝云", Membership = membershipUser.Membership });

            SystemUser systemUserTest = new SystemUser();
            systemUserTest.Name = Guid.NewGuid().ToString();
            membershipUser = organizationMembershipService.Create(
                new Organizations.Models.OrganizationUnit { Name = "3Cloud测试团队", Email = "huay2@samples.cn" },
                new Security.FormsUserCredential
                {
                    AccountName = "3Cloud测试团队",
                    Email = "huay2@samples.cn",
                    UserName = "3cloud_test",
                    Password = "123456",
                    Provider = "Inovout",
                    Identifier = Guid.NewGuid().ToString(),
                    SystemUser = systemUserTest
                }, MembershipUserStatus.Activate);
            membershipUser.Status = Memberships.MembershipUserStatus.Activate;
            membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);
            projectService.Save(new Inovout.Cloud.Models.Project { Name = "3cloud_test", Title = "三宝云测试项目", Membership = membershipUser.Membership });

            SystemUser systemUserDev = new SystemUser();
            systemUserDev.Name = Guid.NewGuid().ToString();
            membershipUser = organizationMembershipService.Create(
                new Organizations.Models.OrganizationUnit { Name = "3Cloud开发团队", Email = "huay3@samples.cn" },
                new Security.FormsUserCredential
                {
                    AccountName = "3Cloud开发团队",
                    Email = "huay3@samples.cn",
                    UserName = "3cloud_dev",
                    Password = "123456",
                    Provider = "Inovout",
                    Identifier = Guid.NewGuid().ToString(),
                    SystemUser = systemUserDev
                }, MembershipUserStatus.Activate);
            membershipUser.Status = Memberships.MembershipUserStatus.Activate;
            membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);
            projectService.Save(new Inovout.Cloud.Models.Project { Name = "3cloud_dev", Title = "三宝云开发项目", Membership = membershipUser.Membership });

            SystemUser systemUserDataWarehouse = new SystemUser();
            systemUserDataWarehouse.Name = Guid.NewGuid().ToString();
            membershipUser = organizationMembershipService.Create(
                new Organizations.Models.OrganizationUnit { Name = "3Cloud数据仓库", Email = "huay4@samples.cn" },
                new Security.FormsUserCredential
                {
                    AccountName = "3Cloud数据仓库",
                    Email = "huay4@samples.cn",
                    UserName = "3cloud_dataWarehouse",
                    Password = "123456",
                    Provider = "Inovout",
                    Identifier = Guid.NewGuid().ToString(),
                    SystemUser = systemUserDataWarehouse
                }, MembershipUserStatus.Activate);
            membershipUser.Status = Memberships.MembershipUserStatus.Activate;
            membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);
            projectService.Save(new Inovout.Cloud.Models.Project { Name = "3cloud_dataWarehouse", Title = "三宝云数据仓库项目", Membership = membershipUser.Membership });
        }

        //将ObjectOperation导入EnumOperation
        private void ImportObjctOperationToEnumOperation()
        {
            //EnumDescriptor enumDescriptor = EnumDescriptor.Get(typeof(ObjectOperation));
            //foreach (var item in enumDescriptor.EnumItems)
            //{
            //    ComponentRegistry.Resolve<IRepositoryService<EnumOperation>>().Save(new EnumOperation { OperationEnum = item.Value });
            //}
        }
        //创建ProductAndPrivilege
        private void CreateProductAndPrivilegeForStorage()
        {
            //EnumDescriptor enumDescriptor = EnumDescriptor.Get(typeof(ObjectOperation));

            ////resouce 
            //HttpResource resource = new HttpResource { Name = "http://storage.3cloud.dev/storage" };
            //ComponentRegistry.Resolve<IRepositoryService<HttpResource>>().Save(resource);

            ////Privilege
            //Privilege privileg = new Privilege(resource, ComponentRegistry.Resolve<EnumOperationService>().FindByEnum(ObjectOperation.ReadWrite))
            //{
            //    Name = "Storage"
            //};
            //ComponentRegistry.Resolve<IRepositoryService<Privilege>>().Save(privileg);



            ////app  
            //Application application = new Application
            //{
            //    Name = "Storage-app",
            //    Membership = ComponentRegistry.Resolve<MembershipService>().FindFirst(),
            //};
            //application.ApplicationInterfaces = new HashedSet<ApplicationInterface>();
            //ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);


            ////api
            ////HttpApplicationInterface api = new HttpApplicationInterface { TemplateUri = "Storage/v1/{buckname}", HttpMethod = "get" };
            ////api.Privileges = new HashedSet<Privilege>();
            ////api.Privileges.Add(privileg);
            ////ComponentRegistry.Resolve<IRepositoryService<HttpApplicationInterface>>().Save(api);

            ////applicaionUser
            //ApplicationUser appUser = new ApplicationUser { Name = "Storage-AppUser" };
            //ComponentRegistry.Resolve<IRepositoryService<ApplicationUser>>().Save(appUser);

            ////application.ApplicationInterfaces.Add(api);
            //application.ApplicationUser = appUser;
            //ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);

            ////product product-app
            //Product product = new Product { Name = "Storage", Title = "云存储", AssemblyName = "Inovout.Cloud.Storage.Console.WebSite" };
            //product.Applications = new HashedSet<IProductApplication>();
            //product.Applications.Add((IProductApplication)application);
            //ComponentRegistry.Resolve<IRepositoryService<Product>>().Save(product);

        }

        private void CreateProductAndPrivilegeForDataStore()
        {
        //    EnumDescriptor enumDescriptor = EnumDescriptor.Get(typeof(ObjectOperation));

        //    //resouce 
        //    HttpResource resource = new HttpResource { Name = "http://drive.3cloud.dev/Drive" };
        //    ComponentRegistry.Resolve<IRepositoryService<HttpResource>>().Save(resource);

        //    //Privilege
        //    Privilege privileg = new Privilege(resource, ComponentRegistry.Resolve<EnumOperationService>().FindByEnum(ObjectOperation.ReadWrite))
        //    {
        //        Name = "Drive"
        //    };
        //    ComponentRegistry.Resolve<IRepositoryService<Privilege>>().Save(privileg);



        //    //app  
        //    Application application = new Application
        //    {
        //        Name = "Drive-app",
        //        Membership = ComponentRegistry.Resolve<MembershipService>().FindFirst(),
        //    };
        //    application.ApplicationInterfaces = new HashedSet<ApplicationInterface>();
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);


        //    //api
        //    HttpApplicationInterface api = new HttpApplicationInterface { TemplateUri = "DataStorage/v1/{buckname}", HttpMethod = "get" };
        //    api.Privileges = new HashedSet<Privilege>();
        //    api.Privileges.Add(privileg);
        //    ComponentRegistry.Resolve<IRepositoryService<HttpApplicationInterface>>().Save(api);

        //    //appUser
        //    ApplicationUser appUser = new ApplicationUser { Name = "Drive-AppUser" };
        //    ComponentRegistry.Resolve<IRepositoryService<ApplicationUser>>().Save(appUser);

        //    application.ApplicationInterfaces.Add(api);
        //    application.ApplicationUser = appUser;
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);

        //    //product product-app
        //    Product product = new Product { Name = "Drive", Title = "云端硬盘", AssemblyName = "Inovout.Cloud.Drive.Console.WebSite" };
        //    product.Applications = new HashedSet<IProductApplication>();
        //    product.Applications.Add(application);
        //    ComponentRegistry.Resolve<IRepositoryService<Product>>().Save(product);
        }
        private void CreateProductAndPrivilegeForIdentiry()
        {
        //    EnumDescriptor enumDescriptor = EnumDescriptor.Get(typeof(ObjectOperation));

        //    //resouce 
        //    HttpResource resource = new HttpResource { Name = IdentirySettings.DefaultScope, Title = "会员信息" };
        //    ComponentRegistry.Resolve<IRepositoryService<HttpResource>>().Save(resource);

        //    //Privilege
        //    Privilege privileg = new Privilege(resource, ComponentRegistry.Resolve<EnumOperationService>().FindByEnum(ObjectOperation.Execute))
        //    {
        //        Name = "Identity"
        //    };
        //    ComponentRegistry.Resolve<IRepositoryService<Privilege>>().Save(privileg);



        //    //app  
        //    Application application = new Application
        //    {
        //        Name = "Identity-app",
        //        Membership = ComponentRegistry.Resolve<MembershipService>().FindFirst(),
        //    };
        //    application.ApplicationInterfaces = new HashedSet<ApplicationInterface>();
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);

        //    //appUser
        //    ApplicationUser appUser = new ApplicationUser { Name = "Identity-AppUser" };
        //    ComponentRegistry.Resolve<IRepositoryService<ApplicationUser>>().Save(appUser);


        //    //api
        //    HttpApplicationInterface api = new HttpApplicationInterface { TemplateUri = "v1/me", HttpMethod = "get" };
        //    api.Privileges = new HashedSet<Privilege>();
        //    api.Privileges.Add(privileg);
        //    api.Application = application;
        //    ComponentRegistry.Resolve<IRepositoryService<HttpApplicationInterface>>().Save(api);


        //    application.ApplicationInterfaces.Add(api);
        //    application.ApplicationUser = appUser;
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);




        //    //product product-app
        //    Product product = new Product { Name = "Identity", Title = "云标识", AssemblyName = "Inovout.Cloud.Identity.Console.WebSite" };
        //    product.Applications = new HashedSet<IProductApplication>();
        //    product.Applications.Add(application);
        //    ComponentRegistry.Resolve<IRepositoryService<Product>>().Save(product);
        //}
        //private void CreateProductAndPrivilegeForDatabaseCluster()
        //{
        //    //EnumDescriptor enumDescriptor = EnumDescriptor.Get(typeof(ObjectOperation));

        //    ////resouce 
        //    //HttpResource resource = new HttpResource { Name = "http://DatabaseCluster.3cloud.dev", Title = "云数据库信息" };
        //    //ComponentRegistry.Resolve<IRepositoryService<HttpResource>>().Save(resource);

        //    ////Privilege
        //    //Privilege privileg = new Privilege(resource, ComponentRegistry.Resolve<EnumOperationService>().FindByEnum(ObjectOperation.Execute))
        //    //{
        //    //    Name = "DatabaseCluster"
        //    //};
        //    //ComponentRegistry.Resolve<IRepositoryService<Privilege>>().Save(privileg);



        //    //app  
        //    Application application = new Application
        //    {
        //        Name = "DatabaseCluster-app",
        //        Membership = ComponentRegistry.Resolve<MembershipService>().FindFirst(),
        //    };
        //    application.ApplicationInterfaces = new HashedSet<ApplicationInterface>();
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);

        //    //appUser
        //    ApplicationUser appUser = new ApplicationUser { Name = "DatabaseCluster-AppUser" };
        //    ComponentRegistry.Resolve<IRepositoryService<ApplicationUser>>().Save(appUser);


        //    //api
        //    HttpApplicationInterface api = new HttpApplicationInterface { TemplateUri = "row/me", HttpMethod = "get" };
        //    //api.Privileges = new HashedSet<Privilege>();
        //    //api.Privileges.Add(privileg);
        //    api.Application = application;
        //    ComponentRegistry.Resolve<IRepositoryService<HttpApplicationInterface>>().Save(api);


        //    application.ApplicationInterfaces.Add(api);
        //    application.ApplicationUser = appUser;
        //    ComponentRegistry.Resolve<IRepositoryService<Application>>().Save(application);




        //    //product product-app
        //    Product product = new Product { Name = "DatabaseCluster", Title = "云数据库", AssemblyName = "Inovout.Cloud.DatabaseCluster.Console.WebSite" };
        //    product.Applications = new HashedSet<IProductApplication>();
        //    product.Applications.Add(application);
        //    ComponentRegistry.Resolve<IRepositoryService<Product>>().Save(product);
        //}
        //private void CreateProductPermissons()
        //{
        //    Organizations.Memberships.Services.IOrganizationMembershipService organizationMembershipService = ComponentRegistry.Resolve<Organizations.Memberships.Services.IOrganizationMembershipService>();
        //    Cloud.Services.ProjectService projectService = ComponentRegistry.Resolve<Cloud.Services.ProjectService>();

        //    //project
        //    Project project = projectService.FindByName("3cloud");

        //    //appuser
        //    ApplicationUser appUser = null;
        //    foreach (var application in project.Applications)
        //    {
        //        if (application.ApplicationUser != null)
        //        {
        //            appUser = application.ApplicationUser;
        //            break;
        //        }
        //    }

        //    //product
        //    IRepositoryService<Permission> repPermisson = ComponentRegistry.Resolve<IRepositoryService<Permission>>();
        //    Cloud.Services.IProductService productService = ComponentRegistry.Resolve<Cloud.Services.IProductService>();
        //    Product product = productService.FindByNameAndAssemblyName("Identity", "Inovout.Cloud.Identity.Console.WebSite");

        //    //激活
        //    if (null == project.Products)
        //    {
        //        project.Products = new HashedSet<Product>();
        //    }
        //    projectService.ChangeProduct(project, product, true);

        //    // IRepositoryService<User> userService = ComponentRegistry.Resolve<IRepositoryService<User>>();
        //    // User person = userService.FindById(4);
        //    // FormsUser formuser= ComponentRegistry.Resolve<IFormsUserService>().FindByName("qsy ");
        //    // (Restrictions.Eq("Name", userName).IgnoreCase());
        //    User user = (User)appUser;
        //    //permisson
        //    foreach (var application in product.Applications)
        //    {
        //        foreach (var api in application.ApplicationInterfaces)
        //        {
        //            foreach (var privilege in api.Privileges)
        //            {
        //                Permission permisson = new Permission();
        //                permisson.Privilege = privilege;
        //                // permisson.Actors = new HashedSet<IActor>();
        //                //permisson.Actors.Add(appUser);
        //                permisson.Actor = appUser;
        //                repPermisson.Save(permisson);
        //            }
        //        }
        //    }
        }



    }
}

